package com.distribute.application.api.rest.member;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.distribute.application.api.BaseApiController;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.model.Account;
import com.distribute.framework.model.Agent;
import com.distribute.framework.model.AgentConf;
import com.distribute.framework.model.Commodity;
import com.distribute.framework.model.Coupon;
import com.distribute.framework.model.DirectCoupon;
import com.distribute.framework.model.DirectOrder;
import com.distribute.framework.model.Headline;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.Merchant;
import com.distribute.framework.model.Parameter;
import com.distribute.framework.model.ProductDetails;
import com.distribute.framework.model.Region;
import com.distribute.framework.model.TbCarouselConfig;
import com.distribute.framework.model.TbMsg;
import com.distribute.framework.model.TbPartnerConfig;
import com.distribute.framework.model.TbPartnerIncome;
import com.distribute.framework.model.TbPartners;
@RestController(value="api.PageDataController")
public class PageDataController extends BaseApiController{
	/**
	 * 物流查询
	 * @return
	 */
	@RequestMapping("/api/memberUser/queryKD.do")
	public @title("用户查看物流信息") Protocol queryKD(
			@title("订单编号") Long orderId
			){
		Protocol protocol = Protocol.create(true, null);
		DirectOrder order=DirectOrder.where.id(orderId).get();
		String cusString=Utils.config("kd.customer");
		String cusSting=Utils.config("kd.key");
		if (order.getLogisMode()!=null&&!order.getLogisMode().equals("")&&order.getCourier()!=null&&!order.getCourier().equals("")) {
			String result=QueryKD.getQueryKD(order.getCourier(),order.getLogisMode());
			JSONObject jsonObject=JSON.parseObject(result);
			if (jsonObject.getString("message").equals("ok")) {
				protocol.set("type", true);
				protocol.set("result", jsonObject);
			}else {
				protocol.set("type", false);
				protocol.set("msg", "暂无物流信息");
			}
			
		}else {
			protocol.set("type", false);
			protocol.set("msg", "暂无物流信息");
		}
		
		return protocol;
	}
	
	@RequestMapping("/api/memberUser/getNotisLog.do")
	public @title("用户自己的消息") Protocol getNotisLog(
			@title("页码") Integer page,
			@title("页码") Integer size
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setPage(TbMsg.where.tbmMember(member.getUid()).page(page, size), new Protocol.BeanFormat<TbMsg>(){
			@Override
			public Map format(TbMsg t) {
				Map json = t.toJSON(true);
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 进入品牌页面
	 * @return
	 */
	@RequestMapping("/api/memberUser/getBanrdPageData.do")
	public @title("用户获取会员中心数据") Protocol getBanrdPageData(
			@title("品牌编号") Long banrdId
			){
		Protocol protocol = Protocol.create(true, null);
		Long memberId=this.getUid();
		Member member=Member.where.uid(memberId).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Merchant merchant=Merchant.where.id(banrdId).get();
		Parameter parameter=Parameter.where.merchant(banrdId).get();
		protocol.setListByKey("lbs",TbCarouselConfig.where.tbccBrand(banrdId).desc("tbcc_sort").list(-1), new Protocol.BeanFormat<TbCarouselConfig>(){
			@Override
			public Map format(TbCarouselConfig t) {
				Map json = t.toJSON();
				return json;
			}
		});
		protocol.setList(Headline.where.merchant(banrdId).scroll((short)1).list(20), new Protocol.BeanFormat<Headline>(){
			@Override
			public Map format(Headline t) {
				Map json = new HashMap();
				json.put("title", t.getTitle());
				return json;
			}
		});
		List<DirectCoupon> diCoupons=DirectCoupon.where.merchant(banrdId).list(1000);
		List<Coupon> coupons=Coupon.where.member(member.getId()).merchant(banrdId).list(1000);
		List<DirectCoupon> list=new ArrayList<DirectCoupon>();
		for (DirectCoupon directCoupon : diCoupons) {
			if(directCoupon.getCount()>0&&directCoupon.getDeadline().getTime()>=new Date().getTime()){
				Boolean isFlag=true;
				for (Coupon coupon : coupons) {
					if (coupon.getSources()==(short)1) {//定向优惠卷
						if (coupon.getCouponId().toString().equals(directCoupon.getId().toString())) {
							isFlag=false;
							break;
						}
					}
				}
				if(isFlag){
					list.add(directCoupon);
				}
			}
		}
		protocol.set("couponCount", list.size());
		protocol.set("banrd", merchant);
		protocol.set("parameter", parameter);
		return protocol;
	}
	
	/**
	 * 进入品牌代理申请页
	 * @return
	 */
	@RequestMapping("/api/memberUser/getApplyPageData.do")
	public @title("进入品牌代理申请页") Protocol getApplyPageData(
			@title("品牌编号") Long banrdId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.set("agentConf", AgentConf.where.merchant(banrdId).get());
		protocol.set("partnerConfig", TbPartnerConfig.where.tbpcBrand(banrdId).get());
		protocol.set("agent", Agent.where.member(member.getId()).merchant(banrdId).get());
		protocol.set("HHR", TbPartners.where.tbpMember(member.getId()).tbpBrand(banrdId).get());
		protocol.set("parameter", Parameter.where.merchant(banrdId).get());
		List<Region> tAddresses=Region.where.parent((long)0).list(1000);
		List<Map<String, Object>> citys=new ArrayList<Map<String, Object>>();
		for (Region region : tAddresses) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("id", region.getId());
			map.put("name", region.getName());
			map.put("have", 0);
			citys.add(map);
		}
		protocol.set("alist", citys);
		return protocol;
	}
	/**
	 * 进入合伙人收益页面
	 * @return
	 */
	@RequestMapping("/api/memberUser/hehuorenList.do")
	public @title("进入合伙人收益页面") Protocol hehuorenList(
			@title("品牌编号") Long banrdId
			,@title("街道") String jname
			,@title("开始时间") Date start
			,@title("结束时间") Date end
			,@title("页码") Integer page
			,@title("条数") Integer size
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		TbPartnerIncome.M where=TbPartnerIncome.where.tbpiBrand(banrdId).tbpiMember(member.getId());
		if (start!=null) {
			where.tbpiOrderTimeStart(start);
		}
		if (end!=null) {
			where.tbpiOrderTimeEnd(end);
		}
		if (jname!=null) {
			where.tbpiStreet(jname);
		}
		protocol.setPage(where.page(page, size), new Protocol.BeanFormat<TbPartnerIncome>(){
			@Override
			public Map format(TbPartnerIncome t) {
				return t.toJSON();
			}
		});
		return protocol;
	}
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));        
	}
}
